package com.colorful.insuranceproject.commonActivity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.utils.PaymentPasswordInputMethodWrapper;
import com.jungly.gridpasswordview.GridPasswordView;
import com.lovely3x.common.activities.BaseCommonActivity;
import com.lovely3x.common.managements.user.UserManager;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 支付密码输入界面
 * Created by lovely3x on 16/7/30.
 */
public class PaymentPasswordInputActivity extends BaseCommonActivity implements GridPasswordView.OnPasswordChangedListener {

    public static final String EXTRA_PASSWORD = "extra.password";

    private static final int REQUEST_CODE_SET_PAYMENT_PASSWORD = 0x12;

    private PaymentPasswordInputMethodWrapper mPaymentPasswordInputMethod;

    @Override
    protected int getContentView() {
        return 0;
    }

    @Override
    protected View getInflatedContentView() {
        return mPaymentPasswordInputMethod = new PaymentPasswordInputMethodWrapper(this);
    }

    @Override
    protected void onViewInitialized() {
        mPaymentPasswordInputMethod.getGridPasswordView().setOnPasswordChangedListener(this);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!UserManager.getInstance().isSigned()) {
//            if (UserManager.getInstance().getCurrentUser() instanceof SimpleUser) {
//                if (!((SimpleUser) UserManager.getInstance().getCurrentUser()).ispaypwd()) {
//                    //已设置支付密码
//                    launchActivityForResult(ModifyPayPasswordActivity.class, REQUEST_CODE_SET_PAYMENT_PASSWORD, ModifyPayPasswordActivity.EXTRA_TYPE, 0);
//                }
//            }
//        } else {
            launchLoginActivity();
        }
    }

    @Override
    public void applyTranslationStatusBarAndNavigationBar(Tint tint) {
        tint.mStatusBarColor = Color.TRANSPARENT;
        super.applyTranslationStatusBarAndNavigationBar(tint);
    }

    /**
     * 当背景视图被点击后执行
     */
    @OnClick(R.id.v_view_payment_password_input_method_wrapper_background)
    public void onBackgroundClicked() {
        setResult(20001);
        finish();
    }

    /**
     * 当关闭按钮被点击后执行
     */
    @OnClick(R.id.iv_view_payment_password_input_method_wrapper_close)
    public void onCloseClicked() {
        setResult(20001);
        finish();
    }

    /**
     * 忘记支付密码被点击后执行
     */
    @OnClick(R.id.tv_view_payment_password_input_method_wrapper_forgot_password)
    public void onForgotPasswordClicked() {
//        launchActivity(BindPhoneActivity.class, BindPhoneActivity.EXTRA_TYPE, 2);
    }

    @Override
    public void onTextChanged(String psw) {

    }

    @Override
    public void onInputFinish(String psw) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_PASSWORD, psw);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void dispatchOnActivityForResultEvent(int requestCode, int resultCode, Intent data) {
        super.dispatchOnActivityForResultEvent(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_CODE_SET_PAYMENT_PASSWORD://设置支付密码
                if (resultCode == RESULT_OK) {

                } else {
                    finish();
                }
                break;
        }
    }
}
